import { purtcList } from '@/api/url/salesOrderManagement';
import router from '@/router';
import { isNullPm } from '@/utils/baseMethods';
import { nextTick, onMounted, onUnmounted, reactive } from 'vue';
import XEClipboard from 'xe-clipboard';
import { VXETable } from 'vxe-table';
import { dayjs } from '@arco-design/web-vue/es/_utils/date';
import { factoryList, resourceList } from '@api/url/productionManagement';

export default function importConfig() {
  const base = reactive<any>({
    pagination: {
      total: 100,
      current: 1,
      pageSize: 10,
    },
    tableList: null,
    checkFrom: {
      deviceid: null,
      devicemodel: null,
      devicename: null,
      factoryId: null,
      manufacturer: null,
      resourceGroupId: null,
    },
    tableRef: null,
    visible: false,
    desData: null,
    defaultSelecteRowKeys: [],
    tabelValue: false,
    searchBar: '隐藏搜索',
    formHeaderVisible: true,
    formHeaderRef: null,
    tableHeight: null,
    factoryList: [],
  });

  // /**
  //  * @exports defaultValueWriteBack 数据回写绑定
  //  */
  // const defaultValueWriteBack = () => {
  //   base.tabelValue = true;
  // };

  //获取表格数据
  const getTable = () => {
    const obj = {
      size: base.pagination.pageSize,
      current: base.pagination.current,
    };

    const relt = base.checkFrom;
    const steamDom = {};
    if (relt.purchaseDate) {
      relt.purchaseDate = dayjs(relt.purchaseDate).format('YYYYMMDD');
    }

    Object.keys(relt).map((res: any) => {
      if (!isNullPm(relt[res])) {
        steamDom[res] = relt[res];
      }
    });

    const param = Object.assign({}, obj, steamDom);

    resourceList(param).then((res: any) => {
      if (res.code === '200') {
        base.tableList = res.data.list;
        base.pagination.total = res.data.total;
      }
    });
  };

  //页码或页数改变时发生的事件
  const handlePageChange = () => {
    getTable();
  };

  /**
   * @exports menuConfig 表单配置器
   */
  const menuConfig = reactive<any>({
    className: 'my-menus',
    body: {
      options: [
        [{ code: 'copy', name: '复制', prefixIcon: 'vxe-icon-copy', className: 'my-copy-item' }],
        [{ code: 'viewDetails', name: '查看详情', prefixIcon: 'vxe-icon-success-circle-fill', className: 'my-copy-item' }],
      ],
    },
  });
  /**
   * @exports contextMenuBodyClickEvent 表单操作格式
   * @param param0
   */
  const contextMenuBodyClickEvent = ({ menu, row, column }) => {
    switch (menu.code) {
      case 'viewDetails':
        viewDetails(row);
        break;
      case 'copy':
        // 示例
        if (row && column) {
          console.log(row, column);
          if (XEClipboard.copy(row[column.field])) {
            VXETable.modal.message({ content: '已复制到剪贴板！', status: 'success' });
          }
        }
        break;
      default:
        console.log(`点击了 ${menu.name} 选项`, row, column, row[column.field]);
    }
  };

  /**
   * @exports viewDetails 查看采购单详情
   */
  const viewDetails = (param) => {
    router.push({ path: '/purchaseOrderDetails', query: { type: param.type, id: param.id } });
  };

  //监听容器高度变化
  const handleResize = () => {
    let height1;
    if (base.formHeaderRef) {
      height1 = base.formHeaderRef.$el.clientHeight;
    } else {
      height1 = 0;
    }
    const height = height1 + 280;
    base.tableHeight = `calc(100vh - ${height}px)`;
  };

  //获取所有工厂
  const getFactory = () => {
    factoryList().then((res) => {
      if (res.code == 200) {
        let obj;
        const arr = [];
        res.data.forEach((item) => {
          obj = {
            value: item.factoryid,
            label: item.factoryname + '(' + item.factoryno + ')',
          };
          arr.push(obj);
        });
        base.factoryList = arr;
      }
    });
  };

  onMounted(() => {
    getTable();
    getFactory();

    nextTick(() => {
      handleResize();
    });
    window.addEventListener('resize', handleResize);
  });

  onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
  });

  return {
    base,
    menuConfig,
    handlePageChange,
    contextMenuBodyClickEvent,
    viewDetails,
    getTable,
  };
}
